package showgo.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import showgo.modell.Manager;
import showgo.modell.stueck.Rolle;
import showgo.modell.stueck.Stueck;

/**
 * Verwaltung von Pseudorollen; Die uebergebene Pseudorolle wird aufeloest;
 * 
 * @author lutz
 * 
 */
public class PseudoRollenDialog extends JDialog implements ItemListener,
	ActionListener, ListSelectionListener {
    /**
     * 
     */
    private static final long serialVersionUID = 823878931678074640L;

    private List<Rolle> pseudoRollen = new ArrayList<Rolle>();

    private List<Rolle> rollen = new ArrayList<Rolle>();
    private List<Rolle> temp = new ArrayList<Rolle>();

    private String titel;

    private int counter = 0;

    private Stueck stueck;

    private javax.swing.JButton aufloesenButton;
    private javax.swing.JLabel counterLabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel pseudoLabel;
    @SuppressWarnings("rawtypes")
    private javax.swing.JList rollenInSzeneList;
    private javax.swing.JLabel rollenLabel;
    private javax.swing.JLabel szeneLabel;
    private javax.swing.JButton abbrechenButton;

    /**
     * uebergeben werden die Pseudorollen, die Rollen, das Stueck und der Titel;
     * 
     */
    public PseudoRollenDialog(List<Rolle> pseudoRollenInSzene,
	    List<Rolle> rollen, String titel, Stueck stueck) {
	super();
	this.pseudoRollen = pseudoRollenInSzene;
	this.rollen = rollen;
	this.titel = titel;
	this.stueck = stueck;
	setTitle("Pseudorollen aufloesen");
	initComponents();
	zeigePseudoRolle();
    }

    private void zeigePseudoRolle() {
	if (this.pseudoRollen.size() > counter) {
	    pseudoLabel.setText("Pseudorolle: "
		    + pseudoRollen.get(counter).getRollenName());
	    counterLabel.setText("(" + (counter + 1) + " von "
		    + pseudoRollen.size() + ")");
	}
    }

    private void initComponents() {

	setSize(400, 400);
	setResizable(false);
	setLocationRelativeTo(null);
	setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

	pseudoLabel = new javax.swing.JLabel();
	rollenLabel = new javax.swing.JLabel();

	fillList();

	szeneLabel = new javax.swing.JLabel();
	szeneLabel.setFont(new java.awt.Font("Tahoma", 3, 16));
	szeneLabel.setText(this.titel);

	pseudoLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
	pseudoLabel.setText("Pseudorolle: ");

	rollenLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
	rollenLabel.setText("Rollen in dieser Szene:");

	jScrollPane1 = new javax.swing.JScrollPane();
	jScrollPane1.setViewportView(rollenInSzeneList);

	aufloesenButton = new javax.swing.JButton();
	aufloesenButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	aufloesenButton.setText("Aufloesen");
	aufloesenButton.addActionListener(this);

	counterLabel = new javax.swing.JLabel();
	counterLabel.setText("(1 von " + pseudoRollen.size() + ")");

	abbrechenButton = new javax.swing.JButton();
	abbrechenButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	abbrechenButton.setText("Abbrechen");
	abbrechenButton.addActionListener(this);

	javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
		getContentPane());
	getContentPane().setLayout(layout);
	layout.setHorizontalGroup(layout
		.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		.addGroup(
			layout.createSequentialGroup()
				.addGroup(
					layout.createParallelGroup(
						javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
							layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
									layout.createParallelGroup(
										javax.swing.GroupLayout.Alignment.LEADING,
										false)
										.addGroup(
											layout.createSequentialGroup()
												.addGroup(
													layout.createParallelGroup(
														javax.swing.GroupLayout.Alignment.TRAILING,
														false)
														.addComponent(
															jScrollPane1,
															javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
															rollenLabel,
															javax.swing.GroupLayout.Alignment.LEADING,
															javax.swing.GroupLayout.DEFAULT_SIZE,
															javax.swing.GroupLayout.DEFAULT_SIZE,
															Short.MAX_VALUE))
												.addPreferredGap(
													javax.swing.LayoutStyle.ComponentPlacement.RELATED,
													javax.swing.GroupLayout.DEFAULT_SIZE,
													Short.MAX_VALUE)
												.addGroup(
													layout.createParallelGroup(
														javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
															aufloesenButton,
															javax.swing.GroupLayout.Alignment.TRAILING,
															javax.swing.GroupLayout.PREFERRED_SIZE,
															134,
															javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
															abbrechenButton,
															javax.swing.GroupLayout.Alignment.TRAILING,
															javax.swing.GroupLayout.PREFERRED_SIZE,
															134,
															javax.swing.GroupLayout.PREFERRED_SIZE)))
										.addComponent(
											szeneLabel,
											javax.swing.GroupLayout.PREFERRED_SIZE,
											400,
											javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(
											pseudoLabel,
											javax.swing.GroupLayout.PREFERRED_SIZE,
											361,
											javax.swing.GroupLayout.PREFERRED_SIZE)))
						.addGroup(
							layout.createSequentialGroup()
								.addGap(41, 41,
									41)
								.addComponent(
									counterLabel)))
				.addContainerGap(
					javax.swing.GroupLayout.DEFAULT_SIZE,
					Short.MAX_VALUE)));
	layout.setVerticalGroup(layout
		.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		.addGroup(
			layout.createSequentialGroup()
				.addContainerGap()
				.addComponent(szeneLabel,
					javax.swing.GroupLayout.PREFERRED_SIZE,
					24,
					javax.swing.GroupLayout.PREFERRED_SIZE)
				.addGap(34, 34, 34)
				.addComponent(pseudoLabel)
				.addGroup(
					layout.createParallelGroup(
						javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
							layout.createSequentialGroup()
								.addGap(1, 1, 1)
								.addComponent(
									counterLabel)
								.addGap(18, 18,
									18)
								.addComponent(
									rollenLabel)
								.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addComponent(
									jScrollPane1,
									javax.swing.GroupLayout.DEFAULT_SIZE,
									158,
									Short.MAX_VALUE))
						.addGroup(
							javax.swing.GroupLayout.Alignment.TRAILING,
							layout.createSequentialGroup()
								.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED,
									javax.swing.GroupLayout.DEFAULT_SIZE,
									Short.MAX_VALUE)
								.addComponent(
									abbrechenButton,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									46,
									javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(
									aufloesenButton,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									46,
									javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap()))));

	pack();
	aufloesenButton.setEnabled(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
	if (e.getSource() == aufloesenButton) {
	    // zuerst die gewaehlten Rollen bestimmen;
	    int[] index = rollenInSzeneList.getSelectedIndices();

	    // Textmenge auf die gewaehlten Rollen aufteilen;
	    for (int i : index) {
		temp.get(i).setTextMenge(
			temp.get(i).getTextMenge()
				+ pseudoRollen.get(counter).getTextMenge());
	    }

	    // Pseudorolle aus Rollen-List entfernen;
	    entfernePseudorolle();

	    ladeNaechstePsRolle();
	} else if (e.getSource() == abbrechenButton) {
	    ladeNaechstePsRolle();
	}
    }

    private void ladeNaechstePsRolle() {
	// naechste Pseudorolle laden, falls vorhanden
	counter += 1;
	if (pseudoRollen.size() > counter) {
	    zeigePseudoRolle();
	    aufloesenButton.setEnabled(false);
	} else {
	    dispose();
	}
    }

    private void entfernePseudorolle() {
	for (int i = 0; i < rollen.size(); i++) {
	    if (rollen.get(i).getRollenName()
		    .equals(pseudoRollen.get(counter).getRollenName())) {
		Manager.getInstance().entferneRolleAusStueck(
			pseudoRollen.get(counter), stueck);
		break;
	    }
	}
	Manager.getInstance().entferneRolleAusInszenierung(
		pseudoRollen.get(counter), stueck);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    private void fillList() {
	DefaultListModel daten = new DefaultListModel();
	rollenInSzeneList = new JList(daten);
	rollenInSzeneList
		.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
	if (!this.rollen.isEmpty()) {
	    for (int i = 0; i < rollen.size(); i++) {
		if (!pseudoRollen.contains(rollen.get(i))) {
		    daten.addElement(rollen.get(i).getRollenName());
		    // Liste zeiger enthaelt die Positionen der Rollen;
		    temp.add(rollen.get(i));
		}
	    }
	}
	rollenInSzeneList.setModel(daten);
	rollenInSzeneList.addListSelectionListener(this);
    }

    @Override
    public void itemStateChanged(ItemEvent arg0) {

    }

    /*
     * (non-Javadoc) Der Button zum Aufloesen wird erst anwaehlbar, wenn auch
     * eine Rolle ausgewaehlt wurde.
     * 
     * @see
     * javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event
     * .ListSelectionEvent)
     */
    @Override
    public void valueChanged(ListSelectionEvent e) {
	if (rollenInSzeneList.getSelectedIndex() > -1) {
	    aufloesenButton.setEnabled(true);
	}
    }
}
